<?php

namespace app\db\controller;
use think\Validate;

class DbUsercare extends Common
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['changePassword'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
    }

    /**
     * 重置密码
     *
     * @param string $mobile      手机号
     * @param string $newpassword 新密码
     * @param string $captcha     验证码
     */
    public function changePassword()
    {
        if($this->request->isPost()){
            $id = $this->request->request("id");
            $newpassword =str_replace(" ",'',$this->request->request("newpassword"));
            if (!$newpassword || !$id) {
                $this->error(__('Invalid parameters'));
            }
            if (!Validate::is($newpassword, "/^[A-Za-z0-9]{6,20}$/")) {
                $this->error(__("The password is a combination of 6-20 letters or numbers"));
            }
            //模拟一次登录
            $this->auth->direct($id);
            $ret = $this->auth->changepwd($newpassword, '', true);
            if ($ret) {
                $this->success(__('Change password successful'));
            } else {
                $this->error($this->auth->getError());
            }
        }else{
            $this->error(__('No Post request'));
        }
    }





}
